//
//  LOCoder.m
//  LegalObserver
//
//  Created by Roxanne Brittain on 11/6/11.
//  Copyright (c) 2011 Digifit. All rights reserved.
//

#import "LOCoder.h"

@implementation LOCoder
@synthesize context;

+ (LOCoder*)coder {
	return [LOCoder new];
}

- (id)init {
    self = [super init];
    if (self) {
        self.context = [NSMutableDictionary dictionary];
    }
    
    return self;
}

#pragma mark Encoding:

- (void)encodeValueOfObjCType:(const char *)type at:(const void *)addr {
}

- (void)encodeObject:(id)objv forKey:(NSString *)key {
	if (objv == nil) return;
	
	if ([objv isKindOfClass:[NSString class]] || 
		[objv isKindOfClass:[NSDictionary class]] ||
		[objv isKindOfClass:[NSNumber class]]) {
	} else if ([objv isKindOfClass:[NSArray class]]) {
		NSMutableArray *array = [NSMutableArray array];
		
		NSUInteger i, count = [objv count];
		for (i = 0; i < count; i++) {
			LOCoder *coder = [LOCoder new];
			[[objv objectAtIndex:i] encodeWithCoder:coder];
			[array addObject:coder.context];
		}
		
		objv = array;
	} else if ([objv conformsToProtocol:@protocol(NSCoding)]) {
		LOCoder *coder = [LOCoder new];
		[objv encodeWithCoder:coder];
		objv = coder.context;
	}
	
	[self.context setObject:objv forKey:key];
}

- (void)encodeBool:(BOOL)boolv forKey:(NSString *)key {
	[self.context setObject:[NSString stringWithFormat:@"%d", boolv] forKey:key];
}
- (void)encodeInt:(int)intv forKey:(NSString *)key {
	[self.context setObject:[NSString stringWithFormat:@"%d", intv] forKey:key];
}
- (void)encodeFloat:(float)realv forKey:(NSString *)key {
	NSString *value = [NSString stringWithFormat:@"%.2f", realv];
	[self.context setObject:value forKey:key];
}

#pragma mark Decoding:

- (id)decodeObjectForKey:(NSString *)key {
	return [self.context objectForKey:key];
}
- (BOOL)decodeBoolForKey:(NSString *)key {
	NSString *value = [self.context objectForKey:key];
	if ([value intValue]) {
		return YES;
	}
	return NO;
}
- (int)decodeIntForKey:(NSString *)key {
	NSString *value = [self.context objectForKey:key];
	return [value intValue];
}
- (float)decodeFloatForKey:(NSString *)key {
	NSString *value = [self.context objectForKey:key];
	return [value floatValue];
}

@end
